<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        const obj = {
            uname: 'ikun',
            hobby: ['sing', 'dance', 'rap'],
            age: '2.5',
            fans: {
                uname: '小黑子',
                active: '露出了鸡脚'
            }
        }

        // for in 浅拷贝
        // const o = {}
        // for (let k in obj) {
            
        //     // console.log(obj[k])
        //     o[k] = obj[k] 相当于o.uname = objuname的值，循环3次，都添加上

        // }
        // console.log(o)

        // 递归深拷贝
       const newObj = {}
        function deepClone(newObj,oldObj)  {
            for(let key in oldObj){
                // 判断每项是不是数组
                if(oldObj[key] instanceof Array){
                    // 准备空数组来接
                    newObj[key] = []
                    // 再次进入函数一层层挖
                    deepClone(newObj[key],oldObj[key])
                } else if (oldObj[key] instanceof Object) {
                    newObj[key] = {}
                    // 再次进入函数一层层挖
                    deepClone(newObj[key],oldObj[key])
                } else {
                    newObj[key] = oldObj[key]
                }
                 
            }
        }
        deepClone(newObj,obj)

        // 深拷贝 json
    //    const newobj = JSON.parse(JSON.stringify(obj))
       console.log(newObj)
       newObj.hobby[1] = '背带裤'
       console.log(obj);

    </script>
</body>
</html>